package com.example.wangyuyuan.androidframe.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.wangyuyuan.androidframe.constant.Constant;

import java.util.Date;
import java.util.Set;

/**
 * Created by wangyuyuan on 2017/5/9.
 */

public class SharedPrefUtil {

    private static final String PREF_GLOBAL = "pref_global";
    private volatile static SharedPrefUtil INSTANCE = null;
    private Context mContext;

    //获取单例
    public static SharedPrefUtil getInstance() {
        if (INSTANCE == null) {
            synchronized (SharedPrefUtil.class) {
                if (INSTANCE == null) {
                    INSTANCE = new SharedPrefUtil();
                }
            }
        }
        return INSTANCE;
    }

    public void init(Context context) {
        this.mContext = context;
    }

    private SharedPreferences getSharedPref() {
        return mContext.getSharedPreferences(PREF_GLOBAL, Context.MODE_PRIVATE);
    }

    /**
     * 设置请求日期
     *
     * @return true-弹出更新提示框，false-不弹出更新提示框
     */
    public void setRequestDate(Date date) {
        getSharedPref()
                .edit()
                .putString("requestDate", DateUtils.parseDate(date, "yyyy-MM-dd HH:mm:ss"))
                .apply();
    }

    /**
     * 获取上一次的请求日期，判断是否进行请求获取新版本
     *
     * @param context Context
     * @return true-请求，false-不请求
     */
    public boolean isRequestToUpdate(Context context) {
        //requestDate上一次的请求时间
        String requestDate = getSharedPref().getString("requestDate", null);
        //上一次的请求时间为null，return true
        if (requestDate == null) return true;

        Date tem = DateUtils.parseDate(requestDate, "yyyy-MM-dd HH:mm:ss");
        //上一次的请求时间不正确，return true
        if (tem == null) return true;

        long lastTime = tem.getTime();
        long daysTime = new Date().getTime();
        //今天的毫秒值-昨天的毫秒值>24 * 3600000，大于一天
        return daysTime - lastTime > 24 * 3600000;
    }

    public String getSid() {
        return getString(Constant.SID_USER);
    }

    public void setSid(String sid) {
        putString(Constant.SID_USER, sid);
    }

    public String getToken() {
        return getString(Constant.TOKEN_USER);
    }
    public void setToken(String token) {
        putString(Constant.TOKEN_USER,token);
    }
    public void putInt(String key, int value) {
        getSharedPref().edit().putInt(key, value).apply();
    }

    public int getInt(String key) {
        return getSharedPref().getInt(key, 0);
    }

    public int getInt(String key, int defaultValue) {
        return getSharedPref().getInt(key, defaultValue);
    }

    public void putString(String key, String value) {
        getSharedPref().edit().putString(key, value).apply();
    }

    public String getString(String key) {
        return getSharedPref().getString(key, null);
    }

    public String getString(String key, String defaultValue) {
        return getSharedPref().getString(key, defaultValue);
    }

    public Set<String> getStringSet(String key, Set<String> defaultValue) {
        return getSharedPref().getStringSet(key, defaultValue);
    }

    public void putStringSet(String key, Set<String> defaultValue) {
        getSharedPref().edit().putStringSet(key, defaultValue).apply();
    }
}
